from Pickles import *

class InterfaceBaseDonnees(object):
    def __init__(self, Service, Communication):
        self.Service = Service
        self.Communication = Communication
        
    def ObtenirTable(self):
        if self.Service == "Client" :
            return pickle.loads(self.Communication.ObtenirTable())
        elif self.Service == "Serveur" :
            return pickle.dumps(self.Communication.ObtenirTable())
        
    def ObtenirChamp(self, NomTable):
        if self.Service == "Client":
            return pickle.loads(self.Communication.ObtenirChamp(pickle.dumps(NomTable)))
        elif self.Service == "Serveur":
            return pickle.dumps(self.Communication.ObtenirChamp(pickle.loads(NomTable)))

    def EffacerTable(self, NomTable):
        pass

    def EntrerEnregistrement(self, LaTable):
        if self.Service == "Client":
            if not isinstance(LaTable, Table):
                return [0, "Envoyer comme argument un objet Table"]
            else:
               return pickle.loads(self.Communication.EntrerEnregistrement(pickle.dumps(LaTable)))
        elif self.Service == "Serveur" :
                return pickle.dumps(self.Communication.EntrerEnregistrement(pickle.loads(LaTable)))

    def ObtenirEnregistrement(self, LaTable):
          if self.Service == "Client":
            if not isinstance(LaTable, Table):
                return [0, "Envoyer comme argument un objet Table"]
            else:
                return pickle.loads(self.Communication.ObtenirEnregistrement(pickle.dumps(LaTable)))
          elif self.Service == "Serveur" :
            return pickle.dumps(self.Communication.ObtenirEnregistrement(pickle.loads(LaTable)))

    def Commit(self):
         if self.Service == "Client":
            return pickle.loads(self.Communication.Commit())
         elif self.Service == "Serveur":
            return pickle.dumps(self.Communication.Commit())
    
    def CreerTable(self, LaTable):
        if self.Service == "Client":
            if not isinstance(LaTable, Table):
                return [0, "Envoyer comme argument un objet Table"]
            else:
               return pickle.loads(self.Communication.CreerTable(pickle.dumps(LaTable)))
        elif self.Service == "Serveur" :
                return pickle.dumps(self.Communication.CreerTable(pickle.loads(LaTable)))
            
    